package com.hopu.mybatis.dao.impl;

import com.hopu.mybatis.dao.BookMapper;
import com.hopu.mybatis.entity.TbBook;
import com.hopu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookMapperImpl implements BookMapper{

    //获得DAO的代理对象
    private BookMapper bookMapper =
            MyBatisUtil.getSession().getMapper(BookMapper.class);

    @Override
    public void add(TbBook book) {
        bookMapper.add(book);
    }

    @Override
    public void update(TbBook book) {
        bookMapper.update(book);
    }

    @Override
    public void delete(Long bookId) {
        bookMapper.delete(bookId);
    }

    @Override
    public TbBook selectById(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public List<TbBook> selectAll() {
        return bookMapper.selectAll();
    }

    @Override
    public List<TbBook> selectByBook(TbBook book) {
//        //实现多条件的查询，需要用到sql的拼接
//        SqlSession session = MyBatisUtil.getSession();
//        String sql = "select * from tb_book where 1=1 ";
//        if(book.getBookName() != null && !book.getBookName().equals("")){
//            sql += "and book_name='" + book.getBookName()+"'";
//        }
//        if(book.getBookAuthor() != null && !book.getBookAuthor().equals("")){
//            sql += "and book_author='" + book.getBookAuthor()+"'";
//        }
//        if(book.getBookPrice() > 0){
//            sql += "and book_price=" + book.getBookPrice();
//        }
//        List<TbBook> books = session.selectList(sql);
        return bookMapper.selectByBook(book);
    }

    @Override
    public List<TbBook> selectByAuthorNames(List<String> authorNames) {
        return bookMapper.selectByAuthorNames(authorNames);
    }
}
